// uView 把它所有的工具类，都放在了 uni.$u 这个对象上
const http = uni.$u.http

// 用户注册
const register = (form) => http.post('/api/user/register', {
	...form,
	projectId: 155
})

// 用户登录
const login = (form) => http.post('/api/user/login', {
	...form
})

// 查看用户信息
const getUserInfo = (userid, token) => http.get('/api/user/searchUser', {
	params: {
		id: userid,
		projectId: 155
	},
	header: {
		'x-token': token
	}
})

// 获取分类信息
const getClassify = () => http.get('/api/classify/get', {
	params: {
		projectId: 155
	}
})

// 根据分类id 获取博客
const getBlogsByClassifyId = (id, limit) => http.get('/api/blog/searchByClassifyId', {
	params: {
		id: id,
		offset: 1,
		limit: limit || 10,
		projectId: 155,
	}
})

// 根据ID查找博客
const getBlogById = (id) => http.get('/api/blog/search', {
	params: {
		id,
		projectId: 155,
	}
})

// 创建博客
const createBlog = (info, token) => http.post('/api/blog/create', {
	projectId: 155,
	...info
}, {
	header: {
		'x-token': token
	}
})

// 获取点赞过的博客
const getLikeBlogs = (token) => http.get('/api/user/getLikeBlog', {
	header: {
		'x-token': token
	}
})

// 点赞、取消点赞博客
const likeBlog = (blogid, token) => http.post('/api/blog/likeBlog', {
	projectId: 155,
	id: blogid
}, {
	header: {
		'x-token': token
	}
})

// 获取关注的用户
const getLikeUser = (token) => http.get('/api/user/getLikeUser', {
	header: {
		'x-token': token
	}
})

// 关注，取关用户
const likeUser = (userid, token) => http.post('/api/user/follow', {
	projectId: 155,
	id: userid
}, {
	header: {
		'x-token': token
	}
})

// 获取当前博客评论
const getCurrentBlogComments = (blogid, limit) => http.get('/api/comment/get', {
	params: {
		id: blogid,
		projectId: 155,
		offset: 1,
		limit: limit || 10
	}
})

// 创建评论
const createComment = (blogid, content, token, parent_id) => http.post('/api/comment/create', {
	blogId: blogid,
	projectId: 155,
	content: content,
	parentCommentId: parent_id
}, {
	header: {
		'x-token': token
	}
})

// 编辑个人信息
const editPersonalInfo = (name, avatar, desc, token) => http.put('/api/user/edit', {
	projectId: 155,
	name,
	avatar,
	desc
}, {
	header: {
		'x-token': token
	}
})

// 获取用户自己的博客
const getMyBlogs = (limit, token) => http.get('/api/blog/getMyBlog', {
	params: {
		limit: limit || 10,
		offset: 1,
		projectId: 155
	},
	header: {
		'x-token': token
	}
})

// 获取点赞过的博客
const getLikedBlogs = (token) => http.get('/api/user/getLikeBlog', {
	header: {
		'x-token': token
	}
})

// 获取关注的用户
const getLikedUser = (token) => http.get('/api/user/getLikeUser', {
	header: {
		'x-token': token
	}
})

// 获取浏览博客的历史记录
const getHistory = (limit, token) => http.get('/api/blog/getHistory', {
	params: {
		projectId: 155,
		offset: 1,
		limit: limit || 10,
	},
	header: {
		'x-token': token
	}
})

// 根据关键词搜索博客
const searchBlogs = (word) => http.get('/api/blog/searchByInput', {
	params: {
		keyword: word,
		projectId: 155
	}
})

// 点赞、取消点赞评论
const likeComment = (blogid, commentId, token) => http.post('/api/comment/like', {
	blogId: blogid,
	projectId: 155,
	commentId: commentId
}, {
	header: {
		'x-token': token
	}
})

// 删除评论
const deleteComment = (commentId, token, beRepliedUserId) => http.delete('/api/comment/delete', {
	id: commentId,
	beRepliedUserId: beRepliedUserId,
	projectId: 155
}, {
	header: {
		'x-token': token
	}
})

// 删除博客
const deleteBlog = (blogId, token) => http.delete('/api/blog/deleteBlog', {
	id: blogId,
	projectId: 155
}, {
	header: {
		'x-token': token
	}
})

export {
	register,
	login,
	getUserInfo,
	getClassify,
	getBlogsByClassifyId,
	getBlogById,
	createBlog,
	getLikeBlogs,
	likeBlog,
	getLikeUser,
	likeUser,
	getCurrentBlogComments,
	createComment,
	editPersonalInfo,
	getMyBlogs,
	getLikedBlogs,
	getLikedUser,
	getHistory,
	searchBlogs,
	likeComment,
	deleteComment,
	deleteBlog
}
